#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>


struct ListNode 
{
      int val;
      struct ListNode *next;
};
 

struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB)
{
    struct ListNode* p1 = headA;
    struct ListNode* p2 = headB;
    int a = 0;
    int b = 0;
    while (p1->next)
    {
        a++;
        p1 = p1->next;
    }
    while (p2->next)
    {
        b++;
        p2 = p2->next;
    }
    if (p1 != p2)
        return NULL;
    struct ListNode* slow = headA;
    struct ListNode* fast = headB;
    int c = b - a;
    if (a > b)
    {
        c = a - b;
        fast = headA;
        slow = headB;
    }
    while (c--)
    {
        fast = fast->next;
    }
    while (fast && slow)
    {
        if (fast == slow)
            return slow;
        fast = fast->next;
        slow = slow->next;
    }
    return NULL;
}